package com.github.wxiaoqi.security.biz.modules.community.biz;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.common.cms.request.CmsTopicQueryParam;
import com.github.wxiaoqi.security.biz.common.cms.request.CmsTopicRequestParam;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.biz.modules.comment.entity.Comment;
import com.github.wxiaoqi.security.biz.modules.comment.mapper.CommentMapper;
import com.github.wxiaoqi.security.biz.modules.community.entity.CmsTopic;
import com.github.wxiaoqi.security.biz.modules.community.entity.CmsTopicCustomer;
import com.github.wxiaoqi.security.biz.modules.community.mapper.CmsTopicCustomerMapper;
import com.github.wxiaoqi.security.biz.modules.community.mapper.CmsTopicMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 
 *
 * @author centerroot
 * @email ${email}
 * @date 2018-12-27 09:37:05
 */
@Slf4j
@Service
public class CmsTopicBiz extends BaseBiz<CmsTopicMapper,CmsTopic> {
	
	@Autowired
	private CommentMapper commentMapper;
	@Autowired
	private CmsTopicCustomerMapper topicCustomerMapper;
	
	/**
	 * 查询话题列表
	 * @param param
	 * @return
	 */
	public TableResultResponse<CmsTopic> queryCmsTopics(CmsTopicQueryParam param) {
		log.info("查询话题列表。。。{}"+param);
		Example example =new Example(CmsTopic.class);
		Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(param.getPlatformId())) {
			criteria.andEqualTo("platformId",param.getPlatformId());
		}
		if (!StringUtils.isEmpty(param.getMerchantId())) {
			criteria.andEqualTo("merchantId",param.getMerchantId());
		}
		criteria.andEqualTo("flag", "00");
		example.setOrderByClause("create_time desc");
		Page<CmsTopic> result = PageHelper.startPage(param.getPage(), param.getLimit());
		List<CmsTopic> list = mapper.selectByExample(example);
		if (!StringUtils.isEmpty(param.getCustomerId())) {
			//小程序查询
			if (null != list && list.size() > 0) {
				for (CmsTopic cmsTopic : list) {
					CmsTopicCustomer cmsTopicCustomerReq = new CmsTopicCustomer();
					cmsTopicCustomerReq.setPlatformId(param.getPlatformId());
					cmsTopicCustomerReq.setCustomerId(param.getCustomerId());
					cmsTopicCustomerReq.setTopicId(cmsTopic.getId());
					CmsTopicCustomer cmsTopicCustomer = topicCustomerMapper.selectOne(cmsTopicCustomerReq);
					if (null != cmsTopicCustomer) {
						if ("0".equals(cmsTopicCustomer.getFollowStatus())) {
							cmsTopic.setFollow("00");
						}else {
							cmsTopic.setFollow("01");
						}
					}else {
						cmsTopic.setFollow("01");
					}
				}
			}
		}
		TableResultResponse<CmsTopic> table = new TableResultResponse<>(result.getTotal(), list);
		return table;
	}
	
	/**
	 * 话题详情
	 * @param bean
	 * @return
	 */
	public Map<String, Object> topicInfo(CmsTopicQueryParam bean){
		 log.info("【话题详情】。。。请求参数{}"+bean);
		 Map<String, Object> respMap = new HashMap<>();
		 CmsTopic configReq = new CmsTopic();
		 CmsTopic configRes = new CmsTopic();
		 try {
			 BeanUtils.copyProperties(bean,configReq);
			 configRes = mapper.selectOne(configReq);
			 if (null != configRes) {
				Example example =new Example(Comment.class);
				Criteria criteria = example.createCriteria();
				if (!StringUtils.isEmpty(bean.getPlatformId())) {
					criteria.andEqualTo("platformId",bean.getPlatformId());
				}
				criteria.andEqualTo("commentTargetId", configRes.getId());
				criteria.andEqualTo("commentModuleNo", "04");
				example.setOrderByClause("comment_time DESC");
				Integer count1 = commentMapper.selectCountByExample(example);
				List<Comment> comments = commentMapper.selectByExample(example);
				configRes.setCommentCount(count1);
				configRes.setComments(comments);
				 respMap.put("code", BizResponseCode.OK.getCode());
				 respMap.put("message", BizResponseCode.OK.getMessage());
				 respMap.put("topic", configRes);
			 }else {
				 respMap.put("code", BizResponseCode.FUNCTION_NO_EXIST.getCode());
				 respMap.put("message", BizResponseCode.FUNCTION_NO_EXIST.getMessage());
			 }
		 } catch (Exception e) {
			e.printStackTrace();
			log.info("【话题详情】  报错：" + e.getMessage());
			respMap.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			respMap.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return respMap;
		 }
		 log.info("【话题详情】   响应参数：{}", respMap);
		 return respMap;
	}
	
	
	
	/**
	 * 添加
	 * @param bean
	 * @return
	 */
	public Map<String, Object> addCmsTopic(CmsTopicRequestParam bean){
		 log.info("【添加话题】。。。请求参数{}"+bean);
		 CmsTopic config = new CmsTopic();
		 Map<String, Object> respMap = new HashMap<>();
		 try {
			 BeanUtils.copyProperties(bean,config);
			 config.setId(String.valueOf(SnowFlake.getId()));
			 config.setCreateTime(new Date());
			 config.setFlag("00");
			 //create_by
			 config.setCreateBy(bean.getPlatformId());
			 if (mapper.insert(config) > 0) {
				respMap.put("code", ResponseCode.OK.getCode());
				respMap.put("message", ResponseCode.OK.getMessage());
			 }
		 } catch (Exception e) {
			e.printStackTrace();
			log.info("【添加话题】  报错：" + e.getMessage());
			respMap.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			respMap.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return respMap;
		 }
		 log.info("【添加话题】   响应参数：{}", respMap);
		 return respMap;
	}
	
	
	/**
	 * 修改
	 * @param bean
	 * @return
	 */
	public Map<String, Object> updateCmsTopic(CmsTopicRequestParam bean){
		 log.info("【修改话题】。。。请求参数{}"+bean);
		 CmsTopic config = new CmsTopic();
		 Map<String, Object> respMap = new HashMap<>();
		 try {
			 BeanUtils.copyProperties(bean,config);
			 if (mapper.updateByPrimaryKeySelective(config) > 0) {
				respMap.put("code", ResponseCode.OK.getCode());
				respMap.put("message", ResponseCode.OK.getMessage());
			}
		 } catch (Exception e) {
			e.printStackTrace();
			log.info("【修改话题】  报错：" + e.getMessage());
			respMap.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			respMap.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return respMap;
		 }
		 log.info("【修改话题】   响应参数：{}", respMap);
		 return respMap;
	}
	
	
	/**
	 * 删除
	 * @param bean
	 * @return
	 */
	public Map<String, Object> deleteCmsTopic(CmsTopicRequestParam bean){
		log.info("【删除话题】。。。请求参数{}"+bean);
		CmsTopic config = new CmsTopic();
		 Map<String, Object> respMap = new HashMap<>();
		 try {
			 BeanUtils.copyProperties(bean,config);
			 config.setFlag("01");
			 if (mapper.updateByPrimaryKeySelective(config) > 0) {
				respMap.put("code", ResponseCode.OK.getCode());
				respMap.put("message", ResponseCode.OK.getMessage());
			}
		 } catch (Exception e) {
			e.printStackTrace();
			log.info("【删除话题】   报错：" + e.getMessage());
			respMap.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			respMap.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return respMap;
		 }
		 log.info("【删除话题】   响应参数：{}", respMap);
		 return respMap;
	}
}